﻿using AutoMapper;
using Entity.WMS.DTO.WarehouseSale;
using Entity.WMS.View.WarehouseSale;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using static Common.Enum.StateEnum;

namespace Service.WMS
{
    public class WarehouseSaleService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public WarehouseSaleService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取销售出库列表
         */
        public async Task<RequestResponseModel<PageResult<WarehouseSaleView>>> GetWarehouseSaleList(WarehouseSaleSearchDTO warehouseSaleSearchDTO)
        {
            //筛选
            var query = _context.WarehouseSaleView
                .WhereIF(a => a.CardNo.Contains(warehouseSaleSearchDTO.CardNo), !string.IsNullOrEmpty(warehouseSaleSearchDTO.CardNo))
                .WhereIF(a => a.GoodsCode.Contains(warehouseSaleSearchDTO.Goods) || a.GoodsName.Contains(warehouseSaleSearchDTO.Goods) || a.GoodsSpec.Contains(warehouseSaleSearchDTO.Goods), !string.IsNullOrEmpty(warehouseSaleSearchDTO.Goods))
                .WhereIF(a => a.WarehouseCode.Contains(warehouseSaleSearchDTO.Warehouse) || a.WarehouseName.Contains(warehouseSaleSearchDTO.Warehouse), !string.IsNullOrEmpty(warehouseSaleSearchDTO.Warehouse))
                .WhereIF(a => a.WarehousePositionCode.Contains(warehouseSaleSearchDTO.WarehousePosition) || a.WarehousePositionName.Contains(warehouseSaleSearchDTO.WarehousePosition), !string.IsNullOrEmpty(warehouseSaleSearchDTO.WarehousePosition))
                ;

            //返回列表
            var warehouseSale = await query
                .OrderBy(a => a.CreateTime)
                .Page(warehouseSaleSearchDTO.CurrentPage, warehouseSaleSearchDTO.PageSize)
                .ToListAsync();

            //计算总数
            var pageCount = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<WarehouseSaleView>(warehouseSale, pageCount);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }


        /**
         * 出库
         */
        public async Task<RequestResponseModel<string>> Delivery(string Id)
        {
            // 校验id
            var query = await Valid.ValidId(_context.WarehouseSale, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            // 校验状态
            if (query.Number_Shipped >= query.Number_Delivery)
            {
                return RequestResponse.CreateResponse(400, "已全部发货", Id);
            }
            query.Number_Shipped += query.Number_Delivery;

            var item = await _context.SaleDeliveryOrderItem.FirstOrDefaultAsync(a => a.Id == query.SaleDeliveryOrderItemId);
            if (item != null)
            {
                var order = await _context.SaleDeliveryOrder.FirstOrDefaultAsync(a => a.Id == item.SaleDeliveryOrderId);
                if (order != null)
                {
                    order.Status = SaleType.已发货;
                }
                var warehouseBalance = await _context.WarehouseBalance.FirstOrDefaultAsync(a => a.GoodsId == item.GoodsId);
                if (warehouseBalance == null)
                {
                    return RequestResponse.CreateResponse(400, "可出库库存不足", Id);
                }
                else
                {
                    warehouseBalance.Number_End -= item.Number_Delivery;
                }
            }

            // 修改并保存数据库
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "发货成功", Id);
        }
    }
}
